/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/




// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://opendomo.com
// date: January 2007
//
// CHANGES:
//
// ----------------------------------------------------------------------------




#ifndef __PS3X8_H__
#define __PS3X8_H__


#ifdef __cplusplus
extern "C" {
#endif

   
/* Lectura del estado de las puertas */
#define PS3X8_GET_IO1(data) (data & 0x01 ? 1 : 0)
#define PS3X8_GET_IO2(data) (data & 0x02 ? 1 : 0)
#define PS3X8_GET_IO3(data) (data & 0x04 ? 1 : 0)
#define PS3X8_GET_IO4(data) (data & 0x08 ? 1 : 0)
#define PS3X8_GET_IO5(data) (data & 0x10 ? 1 : 0)
#define PS3X8_GET_IO6(data) (data & 0x20 ? 1 : 0)
#define PS3X8_GET_IO7(data) (data & 0x40 ? 1 : 0)
#define PS3X8_GET_IO8(data) (data & 0x80 ? 1 : 0)

/* Modificacion del estado de las puertas */
#define PS3X8_SET_IO1(value,data) if(value) data |= 0x01; else data &= ~0x01;
#define PS3X8_SET_IO2(value,data) if(value) data |= 0x02; else data &= ~0x02;
#define PS3X8_SET_IO3(value,data) if(value) data |= 0x04; else data &= ~0x04;
#define PS3X8_SET_IO4(value,data) if(value) data |= 0x08; else data &= ~0x08;
#define PS3X8_SET_IO5(value,data) if(value) data |= 0x10; else data &= ~0x10;
#define PS3X8_SET_IO6(value,data) if(value) data |= 0x20; else data &= ~0x20;
#define PS3X8_SET_IO7(value,data) if(value) data |= 0x40; else data &= ~0x40;
#define PS3X8_SET_IO8(value,data) if(value) data |= 0x80; else data &= ~0x80;
                                   
/**
 * @brief Conecta con la placa y verifica la conexion
 *
 * @param serial: puerto serie al que conectar
 * @return int: descriptor del puerto serie o -1 en caso de error
 */
int ps3x8_connect(const char *serial);

/*
 * @brief Se desconecta de la placa
 *
 * @param fd: Descriptor del puerto serie al que se esta conectado
 */
void ps3x8_disconnect(int fd);

/**
 * @brief Lee el estado de las puertas digitales de la placa
 *
 * @param fd: descriptor del puerto serie
 * @param data: variable donde se guardan los datos leidos
 * @return int: 0 en caso de exito, -1 en caso de error 
 */
int ps3x8_read_digital_data(int fd, unsigned int *data);


/**
 * @brief Envia a la placa los datos a establecer en las puertas digitales
 *
 * @param fd: descriptor del puerto serie
 * @param data: variable donde se guardan los datos leidos
 * @return int: 0 en caso de exito, -1 en caso de error 
 */
int ps3x8_write_digital_data(int fd, unsigned int data);

#ifdef __cplusplus
}
#endif
#endif


